const app = getApp()
const db = wx.cloud.database()
const directionCollection = db.collection('pc_directions')
Page({
  data:{
    user:null,
    list:[],
    directions:[],
    nums:[],
    tab:0,
    result:''
  },
  async onLoad(options){
    let res = await directionCollection.get()
    let directions = res.data.map(v=>{
      return v.name
    })
    let nums = res.data.map(v=>{
      return v.num
    })
    res = await wx.cloud.callFunction({
      name:"pc_statistics"
    })
    console.log('pc_statistics',res)
    console.log('user',app.globalData.user)
    let result = res.result.list.find(v=>{
      return v.name == app.globalData.user.name
    }).choosen
    console.log('result',result)
    let tab = directions.indexOf(result)
    let list = []
    directions.forEach(v=>{
      list.push(res.result.list.filter(vv=>{
        return vv.choosen == v
      }))
    })
    this.setData({
      user:app.globalData.user,
      list,
      result,
      tab,
      directions,
      nums
    })
  },
  changeTab(e){
    this.setData({
      tab:e.detail.current
    })
  },
  changeItem(e){
    console.log(e)
    this.setData({
    tab:e.target.dataset.item
    })
  },
  rank(){
    wx.navigateTo({
      url:'/pages/work18/rank/rank'
    })
  }
})